package com.weishi.project.training.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.utils.StringUtils;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.training.domain.Sportsman;
import com.weishi.project.training.domain.SportsmanBodyFunction;
import com.weishi.project.training.service.ISportsmanBodyFunctionService;
import com.weishi.project.training.service.ISportsmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(value = "PC端 运动员 身体机能 接口",description = "PC端 运动员 身体机能 接口")
@RestController
@RequestMapping("/training/sportsmanBodyFunction")
public class SportsmanBodyFunctionController extends BaseController {

    @Autowired
    private ISportsmanBodyFunctionService sportsmanBodyFunctionService;

    @Autowired
    private ISportsmanService sportsmanService;


    /**
     * 查询sportsmanBodyFunction列表
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanBodyFunction:list')")
    @GetMapping("/list")
    public TableDataInfo list(SportsmanBodyFunction sportsmanBodyFunction) {
        startPage();
        List<SportsmanBodyFunction> list = sportsmanBodyFunctionService.selectSportsmanBodyFunctionList(sportsmanBodyFunction);
        return getDataTable(list);
    }

    @ApiOperation("导出 export 列表")
    @PreAuthorize("@ss.hasPermi('training:sportsmanBodyFunction:export')")
    @Log(title = "sportsmanBodyFunction", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SportsmanBodyFunction sportsmanBodyFunction) {
        List<SportsmanBodyFunction> list = sportsmanBodyFunctionService.selectSportsmanBodyFunctionList(sportsmanBodyFunction);
        ExcelUtil<SportsmanBodyFunction> util = new ExcelUtil<SportsmanBodyFunction>(SportsmanBodyFunction.class);
        return util.exportExcel(list, "运动员身体机能");
    }

    @ApiOperation("导入列表数据")
    @PostMapping("/importData")
    public AjaxResult importTemplate(MultipartFile file) throws Exception
    {
        ExcelUtil<SportsmanBodyFunction> util = new ExcelUtil<>(SportsmanBodyFunction.class);
        List<SportsmanBodyFunction> userList = util.importExcel(file.getInputStream());
        userList.forEach( sportsmanBodyFunction-> {
            if(StringUtils.isNotEmpty(sportsmanBodyFunction.getSportsmanName())){
                Sportsman sportsman = sportsmanService.qrySportsmanByName(sportsmanBodyFunction.getSportsmanName());
                if(sportsman!=null){
                    sportsmanBodyFunction.setSportsmanId(sportsman.getId());
                    sportsmanBodyFunction.setCtime(new Date());
                    if(sportsmanBodyFunction.getStartTime()==null) {
                        sportsmanBodyFunction.setStartTime(new Date());
                    }
                    sportsmanBodyFunction.setSportsmanName(sportsman.getName());
                    sportsmanBodyFunction.setEndTime(sportsmanBodyFunction.getStartTime());
                    sportsmanBodyFunctionService.save(sportsmanBodyFunction);
                }
            }

        });
        return AjaxResult.success();
    }

    @ApiOperation("导入列表模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SportsmanBodyFunction> util = new ExcelUtil<>(SportsmanBodyFunction.class);
        return util.importTemplateExcel("模板数据");
    }

    /**
     * 获取sportsmanBodyFunction详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanBodyFunction:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(sportsmanBodyFunctionService.getById(id));
    }

    /**
     * 新增sportsmanBodyFunction
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult add(@RequestBody SportsmanBodyFunction sportsmanBodyFunction) {
        sportsmanBodyFunction.setCtime(new Date());
        if(sportsmanBodyFunction.getStartTime()==null) {
            sportsmanBodyFunction.setStartTime(new Date());
        }
        sportsmanBodyFunction.setEndTime(sportsmanBodyFunction.getStartTime());

        List<Sportsman> sportsmanList = sportsmanService
                .list(Wrappers.<Sportsman>lambdaQuery()
                        .eq(Sportsman::getId , sportsmanBodyFunction.getSportsmanId()));

        if(sportsmanList.size() > 0){
            sportsmanBodyFunction.setSportsmanName(sportsmanList.get(0).getName());
        }

        sportsmanBodyFunctionService.save(sportsmanBodyFunction);
        return AjaxResult.success();
    }

    /**
     * 修改sportsmanBodyFunction
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult edit(@RequestBody SportsmanBodyFunction sportsmanBodyFunction) {
        if(sportsmanBodyFunction.getStartTime()==null) {
            sportsmanBodyFunction.setStartTime(new Date());
        }
        sportsmanBodyFunction.setEndTime(sportsmanBodyFunction.getStartTime());

        List<Sportsman> sportsmanList = sportsmanService
                .list(Wrappers.<Sportsman>lambdaQuery()
                        .eq(Sportsman::getId , sportsmanBodyFunction.getSportsmanId()));

        if(sportsmanList.size() > 0){
            sportsmanBodyFunction.setSportsmanName(sportsmanList.get(0).getName());
        }

        sportsmanBodyFunctionService.updateById(sportsmanBodyFunction);
        return AjaxResult.success();
    }

    /**
     * 删除sportsmanBodyFunction
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanBodyFunction:remove')")
    @Log(title = "sportsmanBodyFunction", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        sportsmanBodyFunctionService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
    
}